package com.weme.common.util

import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.weme.common.R
import com.weme.common.base.BaseDialog
import java.lang.ref.WeakReference

class LoadingDailog : BaseDialog() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = AlertDialog.Builder(requireActivity()).setView(R.layout.progress_dialog_layout).create()

        dialog.apply {
            window?.setBackgroundDrawableResource(android.R.color.transparent)
        }

        return dialog
    }

    companion object {
        private var dialogReference: WeakReference<LoadingDailog>? = null

        fun init() { // 可以在需要的时候初始化
        }

        fun showDialog(fragmentManager: FragmentManager) {
            val handler = Handler(Looper.getMainLooper())
            handler.post {
                try {
                  //  dismiss() // 关闭已有的对话框
                    dialogReference = WeakReference(LoadingDailog())
                    dialogReference?.get()?.show(fragmentManager, "LoadingDailog")
                } catch (_: Exception) {
                }
            }


        }


        fun dismiss() {
            val handler = Handler(Looper.getMainLooper())
            handler.post {
                dialogReference?.get()?.let {

                        it.dismiss()

                }
                dialogReference = null
            }
        }
    }
}
